using UnityEngine;
using System.Collections;
using UnityEditor;

public class main : MonoBehaviour
{
		public float timeToStart = 0.0f;
		public float speed = 0.5f;
		public float bestTime = 0f;
		public GameObject player;
		private GUIStyle largeFont;
		public GameObject enemy;
		public static bool gameIsRunning = false;
		// Use this for initialization
		void Start ()
		{
				//Instantiate (player, new Vector3(-9,3,0), new Quaternion());
		StartCoroutine ("AddEnemy2");
				this.largeFont = new GUIStyle ();
				this.largeFont.fontSize = 50;
				GameObject.Find ("Sphere").rigidbody2D.isKinematic = true;
				this.bestTime = PlayerPrefs.GetFloat ("Best Time");
		}
	
		// Update is called once per frame
		void Update ()
		{
				//this.speed = 5f - BouncingEnemy.destroyed / 10;
				
				this.timeToStart += Time.deltaTime;
			
				if (collision.ColliderState == true) {
						if (this.bestTime < this.timeToStart) {
								PlayerPrefs.SetFloat ("Best Time", this.timeToStart);
								this.bestTime = this.timeToStart;
						}
						//player.rigidbody2D.velocity = new Vector2(0, 8);
						this.timeToStart = 0.0f;
						this.speed = 0.5f;
						BouncingEnemy.destroyed = 0;
						foreach (GameObject enemyObj in GameObject.FindGameObjectsWithTag("Enemy")) {
								Destroy (enemyObj);
						}
						main.gameIsRunning = false;
						GameObject.Find ("Sphere").rigidbody2D.isKinematic = true;
				}

		}

		void OnGUI ()
		{
				GUI.Label (new Rect (6, -2, 100, 20), "Best: " + this.bestTime.ToString ("F2"));
				EditorGUI.ProgressBar (new Rect (100, 3, 297, 10), this.timeToStart * 100 / 6000, "");
				//	GUI.Label (new Rect (100, 100, 100, 20), "Score: " + this.timeToStart.ToString ("F2"), largeFont);
		}

		private IEnumerator AddEnemy1 ()
		{
				int y = Random.Range (-4, 4); 
				Instantiate (enemy, new Vector3 (8, y, 0), new Quaternion ());
				yield return new WaitForSeconds (this.speed);
				this.WaveHandler ();
				//StartCoroutine ("AddEnemy1");
		}

		private IEnumerator AddEnemy2 ()
		{
				int y = Random.Range (-3, 3);
				for (int i = -4; i <= 4; i++) {
						if (i != y)
								Instantiate (enemy, new Vector3 (8, i, 0), new Quaternion ());
				}
				yield return new WaitForSeconds (1.5f);
				this.WaveHandler ();
				//StartCoroutine ("AddEnemy2");
		}

		private void WaveHandler ()
		{
				if (main.gameIsRunning) {
						switch (Random.Range (0, 4)) {
						case 0:
						case 1:
						case 2:
								StartCoroutine ("AddEnemy1");
								break;
						case 3:
								StartCoroutine ("AddEnemy2");
								break;

						}
				}
		}
}
